<?php

// @formatter:off
/**
 * @file tag.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: tag.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/content/tag.php $
 */
// @formatter:on

class ActionControllerContentTag extends WCActionController {
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;
			global $wcTag;

			if(!$wcProfile->allowedTo(WC_PERM_TAG_VIEW)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You're not allowed to view tags";
			}
			if($check == WC_HTTPERROR_OK && (!$wcTag || !$wcTag->ok())) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_BAD_REQUEST;
				$errno = $check;
				$error = "Invalid tag";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;

		//
		// Loading global pointers.
		global $wcTag;
		global $wcSkinConfig;
		global $wcGroup;
		global $wcSession;
		global $wcUser;
		global $wcProfile;

		$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
		$minCmcInPage = $wcSkinConfig->max_per_page->tag * ($page - 1);
		$maxCmcInPage = $minCmcInPage + $wcSkinConfig->max_per_page->tag;

		$this->_manager->PATH = "{$wcTag->name()}";

		if($wcGroup->isPublic()) {
			$this->_manager->rss_link = "rss.php?group={$wcGroup->id()}&action=".WC_RSS_TAG."&tag=".$wcTag->id();
			$this->_manager->rss_title = "Tag '{$wcTag->name()}' Feeds";
		} elseif($wcSession->isLogged()) {
			$this->_manager->rss_link = "rss.php?group={$wcGroup->id()}&action=".WC_RSS_TAG."&tag={$wcTag->id()}&uhash={$wcUser->rssHash()}";
			$this->_manager->rss_title = "Tag '{$wcTag->name()}' Feeds";
		}

		if(!$wcTag->isSpecial() && $wcProfile->allowedTo(WC_PERM_TAG_RENAME)) {
			$this->_manager->info_name_editable = true;
		}
		//
		// Loading comics list.
		// @{
		$comics = array();
		$comicCount = 0;
		$items = $wcTag->comicsFull();
		uasort($items, "WCComic::NameSort");
		foreach($items as $comic) {
			if($comic->ok()) {
				if($comicCount >= $minCmcInPage && $comicCount < $maxCmcInPage) {
					$comics[] = WCFVBoxes::GetComicPreviewAssigns($comic);
				}
				$comicCount++;
			}
		}
		if(count($comics)) {
			$this->_manager->has_comics = true;
		}
		if($comicCount) {
			$this->_manager->has_any_comics = true;
		}
		$this->_manager->comics = $comics;
		$this->_manager->info_count = $comicCount;
		// @}
		//
		// Loading images list.
		// @{
		$images = array();
		$imagesCount = 0;
		$flowUri = WCFlowManager::I()->urlFlowFor($wcTag);
		$items = $wcTag->imagesFull();
		uasort($items, "WCImage::NameSort");
		foreach($items as $image) {
			if($image->ok()) {
				if($imagesCount >= $minCmcInPage && $imagesCount < $maxCmcInPage) {
					$images[] = WCFVBoxes::GetImagePreviewAssigns($image, false, $flowUri);
				}
				$imagesCount++;
			}
		}
		if(count($images)) {
			$this->_manager->has_images = true;
		}
		if($imagesCount) {
			$this->_manager->has_any_images = true;
		}
		$this->_manager->images = $images;
		$this->_manager->info_icount = $imagesCount;
		// @}
		//
		// Pager configuration.
		if($comicCount > $imagesCount) {
			WCFVPager::GetAssignsIntoManager(ceil($comicCount / $wcSkinConfig->max_per_page->tag));
		} else {
			WCFVPager::GetAssignsIntoManager(ceil($imagesCount / $wcSkinConfig->max_per_page->tag));
		}

		if($wcTag->ok() && !$wcTag->valid_preview) {
			$wcTag->generatePreviews();
		}

		WCViewInfoFactory::LoadInfo($wcTag, true);
		WCViewInfoFactory::LoadInterfacesInfo($wcTag, true);

		return $ok;
	}
}
